package com.deer.wms.ware.task.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;

import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.service.QueryCriteria;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.system.manage.constant.SystemManageConstant;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.dao.ErpPushLogMapper;
import com.deer.wms.ware.task.model.ErpPushLog;
import com.deer.wms.ware.task.model.ErpPushLogInfo;
import com.deer.wms.ware.task.service.ErpPushLogInfoService;
import com.deer.wms.ware.task.service.ErpPushLogService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 1
 * @description 针对表【erp_push_log(回传ERP日志)】的数据库操作Service实现
 * @createDate 2022-06-14 12:59:55
 */
@Service
public class ErpPushLogServiceImpl extends ServiceImpl<ErpPushLogMapper, ErpPushLog> implements ErpPushLogService {
    @Autowired
    private ErpPushLogInfoService erpPushLogInfoService;
    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private CodeRuleService codeRuleService;
    @Override
    @Async
    public void inserBill(OrderToErpBO bean, Map<String, String> map) {
        ErpPushLog erpPushLog = new ErpPushLog();
        List<OrderDetailBO> details = bean.getDetails();
        List<ErpPushLogInfo> infos = new ArrayList<>();
        BeanUtils.copyProperties(bean, erpPushLog);
        erpPushLog.setCode(map.get("code"));
        if (!map.get("code").equals("200")) {
            erpPushLog.setMsg(map.get("msg"));
        }
//        this.save(erpPushLog);
        for (OrderDetailBO detail : details) {
            ErpPushLogInfo info = new ErpPushLogInfo();
            BeanUtils.copyProperties(detail, info);
            info.setErpPushId(erpPushLog.getId());
            info.setBoxCode(detail.getBoxCode());
            infos.add(info);
        }
        erpPushLogInfoService.saveBatch(infos);
    }

    @Override
    public List<ErpPushLog> findPage(QueryCriteria criteria) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper();
        queryWrapper.ne("epl.code", 200);
        return baseMapper.findPage(criteria, queryWrapper);
    }

    @Override
    public List<ErpPushLogInfo> detail(Integer id) {
        return baseMapper.detail(id);
    }




}
